<template>
  <div class="home-container">
    <van-nav-bar fixed>
      <van-button slot="title" round type="info" size="small" icon="search" to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 标签栏模块 -->
    <van-tabs v-model="active" swipeable>
      <van-tab v-for="item in channel_list" :key="item.id" :title="item.name">
        <!-- 文章列表模块 -->
        <ArticleList :channel="item"></ArticleList>
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡包按钮 -->
      <div slot="nav-right" class="right" @click="isChannelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
      <!-- 弹出层 -->
      <van-popup
        close-icon-position="top-left"
        v-model="isChannelEditShow"
        closeable
        position="bottom"
        :style="{ height: '100%' }"
      >
        <ChannelEdit
          :channel="channel_list"
          :active="active"
          @update-active="isEditChannelShow"
        ></ChannelEdit>
      </van-popup>
    </van-tabs>
  </div>
</template>

<script>
// 导入请求方法
import { getUserChannels } from '@/api/Login.js'
import ArticleList from '@/components/home/article-list/index.vue'
import ChannelEdit from '@/components/home/channel-edit/index.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'

export default {
  name: 'Home',
  data() {
    return {
      active: 0,
      channel_list: [], // 频道数组
      isChannelEditShow: false // 弹出层显示状态
    }
  },
  methods: {
    // 请求用户频道
    async loadChannels() {
      /**
       * 1.判断用户是否登录
       * 2.登陆状态请求数据
       * 3.非登录状态--判断是否有本地数据
       * 4.有--读取本地数据
       *   无--请求数据
       */
      try {
        if (!this.user) {
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            this.channel_list = localChannels
          } else {
            const { data } = await getUserChannels()
            this.channel_list = data.data.channels
          }
        } else {
          const { data } = await getUserChannels()
          this.channel_list = data.data.channels
        }
      } catch (error) {
        console.log(error)
        this.$toast('请求用户频道失败')
      }
    },

    /**
     * 弹出层组件自定义事件
     * 参数：点击频道的当前的索引
     * */
    isEditChannelShow(index, isChannelEditShow) {
      this.active = index // 当前选中的频道等于点击的该频道
      this.isChannelEditShow = isChannelEditShow // 弹出层状态
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  // 生命周期函数
  created() {
    this.loadChannels()
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 180px;
  padding-bottom: 100px;
  // 顶部模块
  .van-nav-bar__title {
    max-width: none;
  }
  /deep/.van-tabs--line .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
  }
  .van-button {
    height: 65px;
    width: 556px;
    font-size: 28px;
    border: none;
    background-color: #5babfb;
    .van-icon {
      font-size: 40px;
    }
  }
  // 标签栏模块
  /deep/ .van-tabs__nav {
    padding-bottom: 0;
    border-bottom: 1px solid #edeff3;
    .van-tab {
      min-width: 200px;
      height: 82px;
      font-size: 30px;
      color: #777777;
      border-right: 1px solid #edeff3;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__line {
      bottom: 10px;
      height: 5px;
      // 最高优先级
      width: 40px !important;
      background-color: #3296fa;
    }
    .right {
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      right: 0;
      height: 82px;
      width: 67px;
      opacity: 0.9;
      background-color: #fff;
      i {
        font-size: 40px;
      }
      &::after {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 82px;
        background: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
    .placeholder {
      flex-shrink: unset;
      min-width: 67px;
      height: 82px;
    }
  }
}
</style>
